/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.uc.service.impl;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import com.xy.biz.uc.domain.req.SaveSignatureRequest;
import com.xy.biz.uc.domain.req.UploadSignatureRequest;
import com.xy.lang.domain.Result;
import com.xy.tool.context.UserContextHolder;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.biz.uc.domain.bo.SignatureBO;
import com.xy.biz.uc.domain.query.SignatureQuery;
import com.xy.biz.uc.service.SignatureService;
import com.xy.biz.uc.manager.SignatureManager;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 *
 *
 * @author yzh
 * @date 2021-2-22
 */
@Slf4j
@Service
public class SignatureServiceImpl implements SignatureService {


    @Autowired
    private SignatureManager signatureManager;

    @Override
    public Result<?> saveSignature(SignatureBO signatureBO) {
        if (Objects.nonNull(signatureBO.getSignatureId())) {
            int affectedRows = signatureManager.updateSignature(signatureBO);
            if (affectedRows > 0) {
                return Result.success();
            }
        } else {
            buildSignature(signatureBO);
            int affectedRows = signatureManager.saveSignature(signatureBO);
            if (affectedRows > 0) {
                return Result.success();
            }
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> saveAllSignature(Collection<SignatureBO> collection) {
        int affectedRows = signatureManager.saveAllSignature(collection);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> updateSignature(SignatureBO signatureBO) {
        int affectedRows = signatureManager.updateSignature(signatureBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    /**
     * 更新
     *
     * @param request
     */
    @Override
    public Result<?> uploadSignature(UploadSignatureRequest request) {
        SignatureBO signatureByUserId = getSignatureByUserId();
        if (Objects.isNull(signatureByUserId)) {
            SignatureBO signatureBO = new SignatureBO();
            signatureBO.setSign(request.getSign());
            buildSignature(signatureBO);
            return saveSignature(signatureBO);
        }
        signatureByUserId.setUpdateTime(LocalDateTime.now());
        signatureByUserId.setSign(request.getSign());
        return updateSignature(signatureByUserId);
    }

    @Override
    public Result<?> deleteSignature(Long signatureId) {
        int affectedRows = signatureManager.deleteSignature(signatureId);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public SignatureBO getSignature(Long signatureId) {
        return signatureManager.getSignature(signatureId);
    }

    @Override
    public List<SignatureBO> listSignatureById(Collection<Long> collection) {
        return signatureManager.listSignatureById(collection);
    }

    /**
     * 查找多个
     *
     * @param collection 标识集合
     */
    @Override
    public List<SignatureBO> listSignatureByUserId(Collection<Long> collection) {
        List<SignatureBO> signatureBOS = signatureManager.listSignatureByUserId(collection);
        return signatureBOS;
    }

    @Override
    public List<SignatureBO> listSignature(SignatureQuery query) {
        return signatureManager.listSignature(query);
    }

    @Override
    public int countSignature(SignatureQuery query) {
        return signatureManager.countSignature(query);
    }

    @Override
    public SignatureBO getSignatureByUserId() {
        return signatureManager.getSignatureByUserId();
    }


    void buildSignature(SignatureBO signatureBO) {
        LocalDateTime now = LocalDateTime.now();
        signatureBO.setCreateTime(now);
        signatureBO.setStatus(0);
        signatureBO.setUserId(UserContextHolder.getUserId());
        signatureBO.setUserName(UserContextHolder.getUserName());
        signatureBO.setUpdateTime(now);
        signatureBO.setDeleteFlag(false);
    }

}
